var express = require('express');
var router = express.Router();
var {list,medicalModel,cateModel,types,content,department,date,medicalModel,cateModel,userlistModel}=require('../model/model')
var multiparty=require('multiparty');
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/add_list',async(req,res)=>{
  console.log(1);
  res.send({
    code:200
  })
})

router.get('/get_list',async(req,res)=>{
  let data=await list.find()
  res.send({
    code:200
  })
})



router.get('/medical',async(req,res)=>{
    let data=await medicalModel.find();
    res.send({
        code:200,
        msg:'展示成功',
        data
    })
})

//添加文章
router.post('/addMedical',async(req,res)=>{
  await medicalModel.create(req.body);
  res.send({
      code:200,
      msg:'添加成功',
  })
})

//获取到文章的详情页
router.get('/show/:id',async(req,res)=>{
    let {id}=req.params;
    let data=await medicalModel.find({_id:id})
    res.send({
        code:200,
        msg:'展示成功',
        data
    })
})

//展示分类
router.get('/cate',async(req,res)=>{
    let data=await cateModel.find();
    res.send({
        code:200,
        msg:'展示成功',
        data
    })
})

//添加分类
router.post('/addCate',(req,res)=>{
    cateModel.create(req.body);
    res.send({
        code:200,
        msg:'添加成功'
    })
})

// 展示挂号分类
router.get('/get_types',async(req,res)=>{
  let data=await types.find();
  res.send({
      code:200,
      msg:'展示成功',
      data
  })
})
// 添加挂号分类
router.post('/add_types',(req,res)=>{
  types.create(req.body);
  res.send({
      code:200,
      msg:'添加成功'
  })
})
//上传检查报告或者病例图片
router.post('/uploadImg',(req,res)=>{
    let form=new multiparty.Form();
    form.uploadDir='upload'
    form.parse(req,(a,b,imgData)=>{
        let imgUrl=imgData.file[0].path
        res.send({
            code:200,
            msg:'上传成功',
            imgUrl:'http://127.0.0.1:3000/'+imgUrl
        })
    })
})


//展示用户列表  
router.get('/userlist',async(req,res)=>{
    let data=await userlistModel.find();
    res.send({
        code:200,
        msg:'展示成功',
        data
    })
})
//添加用户列表
router.post('/addUserlist',async(req,res)=>{
    await userlistModel.create(req.body);
    res.send({
        code:200,
        msg:'添加成功'
    })
})





// 展示挂号内容
router.get('/get_content',async(req,res)=>{
  let data=await content.find();
  res.send({
      code:200,
      msg:'展示成功',
      data
  })
})
// 添加挂号内容
router.post('/add_content',(req,res)=>{
  content.create(req.body);
  res.send({
      code:200,
      msg:'添加成功'
  })
})

// 展示科室内容
router.get('/get_department',async(req,res)=>{
  let data=await department.find();
  res.send({
      code:200,
      msg:'展示成功',
      data
  })
})
// 获取单个科室内容
router.get('/dan_department',async(req,res)=>{
  const {_id}=req.query
  let data=await department.find({_id});
  res.send({
      code:200,
      msg:'展示成功',
      data
  })
})
// 添加科室内容
router.post('/add_department',(req,res)=>{
  department.create(req.body);
  res.send({
      code:200,
      msg:'添加成功',
  })
})

// 展示日期
router.get('/get_date',async(req,res)=>{
  let data=await date.find();
  res.send({
      code:200,
      msg:'展示成功',
      data
  })
})
// 添加日期
router.post('/add_date',(req,res)=>{
  date.create(req.body);
  res.send({
      code:200,
      msg:'添加成功'
  })
})
console.log('王志航');
module.exports = router;
